home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
toolfix.arc
/
SORT2.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1985-08-07
|
9KB
|
290 lines
{$C-}
program SortMultipleFiles;
{
TURBO DATABASE TOOLBOX DEMONSTRATION PROGRAM:
How to write a sort routine that can select which file of records
to sort.
Modified: 08/07/85
This program takes the CUSTOMER.DTA and the STOCK.DTA files, sorts
the one requested by the user and displays the sorted records on the
screen.
}
type
NameString = string[25];
CustRec = record
Number: integer;
Name: NameString;
Addr: string[20];
City: string[12];
State: string[3];
Zip: string[5];
end;
ItemRec = record
Number: integer;
Descrip: string[30];
InStock: integer;
Price: real;
end;
var
CustFile : file of CustRec;
Customer : CustRec;
StockFile : file of ItemRec;
Item : ItemRec;
Choice : char;
Results : integer;
{$I SORT.BOX }
procedure ClrEOS(Y : byte);
{ Clear the screen from row Y to 25, then place cursor
on column 1, row Y.
}
var i : integer;
begin
for i := Y to 25 do
begin
GoToXY(1, i);
ClrEOL;
end;
GoToXY(1, Y);
end; { ClrEOS }
procedure OpenFile(var Choice : char);
{ Set up screen, select which file to sort, open data file }
procedure Menu(var Choice : char);
{ Set up screen, select which file to sort. }
begin
ClrScr;
Writeln('TURBO-SORT DEMONSTRATION PROGRAM':56);
Writeln;
Writeln;
Writeln;
Writeln('Turbo-Sort is fast! This program will ring the');
Writeln('bell when the sort starts and then ring it again');
Writeln('when the sort is finished.');
Writeln;
Writeln;
Writeln('Sort');
Writeln('----');
Writeln;
Writeln('Customer file');
Writeln('Stock File');
Writeln;
Write('Enter C or S: ');
repeat
Read(KBD, Choice);
if Choice in [^C, #27] then Halt; { abort program }
Choice := UpCase(Choice);
until Choice in ['C','S'];
ClrEOS(3);
case Choice of { draw header }
'C' : begin
Writeln(' No. Company Name Address',
' City State Zip');
Writeln('--- ---- ------------------------- ',
'-------------------- ------------ -- -----');
Writeln;
end; { C }
'S' : begin
Writeln(' ':10,
' No. Description ',
' Qty Price');
Writeln(' ':10,
'--- ---- ------------------------------ ',
'----- -------');
Writeln;
end; { C }
end; { case }
end; { Menu }
begin { OpenFiles }
Menu(Choice);
Writeln;
Writeln('Opening data file');
case Choice of
'C': begin
Assign(CustFile,'CUSTOMER.DTA');
{$I-}
Reset(CustFile);
end;
'S': begin
Assign(StockFile,'STOCK.DTA');
{$I-}
Reset(StockFile);
end;
end; {case}
{$I+}
if IOresult <> 0 then
begin
Writeln(' -- Cannot find data file.');
Halt; { abort program }
end;
end; { OpenFile }
procedure Inp;
{ This procedure is forward declared in SORT.BOX. It sends
a stream of records to the sort routine. It also keeps the
user informed of how many records have been read.
}
var
rec : integer;
begin
rec := 0;
Writeln;
case Choice of
'C': begin
Writeln('Input routine -- sending ', FileSize(CustFile),
' records to sort:');
repeat
rec := rec + 1;
Write(#13, rec:6);
Read(CustFile,Customer);
SortRelease(Customer);
until EOF(CustFile);
Writeln;
Writeln;
Writeln('Done with input -- sorting ',
FileSize(CustFile),
' records . . .', ^G); { ring bell }
end; { C }
'S': begin
Writeln('Input routine -- sending ', FileSize(StockFile),
' records to sort:');
repeat
rec := rec + 1;
Write(#13, rec:6);
Read(StockFile,Item);
SortRelease(Item);
until EOF(StockFile);
Writeln;
Writeln;
Writeln('Done with input -- sorting ',
FileSize(StockFile),
' records . . .', ^G); { ring bell }
end; { S }
end; { case }
end; { Inp }
function Less;
{ This boolean function specifies sort priority. It is
forward declared in SORT.BOX and has two parameters, X
and Y. Record X is sorted "lower" than Y based on a
comparison between the fields specified below (Name,
Customer number, etc.). Because this function is
called many times, the number of statements in this
function should be kept to a minimum.
}
var
FirstCust: CustRec absolute X; { customer file }
SecondCust: CustRec absolute Y;
FirstItem: ItemRec absolute X; { stock file }
SecondItem: ItemRec absolute Y;
begin
case Choice of { define sort priority }
'C': Less := FirstCust.Number < SecondCust.Number;
'S': Less := (FirstItem.InStock < SecondItem.InStock) or
((FirstItem.InStock = SecondItem.InStock) and
(FirstItem.Price < SecondItem.Price));
end;
end; { Less }
procedure OutP;
{ This procedure is forward declared in SORT.BOX. It
retrieves the sorted objects one-by-one and displays
them on the screen. NOTE: If your terminal does not
provide support for deleting a line, you should
modify the Scroll procedure below.
}
var
i, Line : integer;
procedure Scroll(Line : integer);
{ This procedure controls scrolling during output of records.
If your terminal does not support line delete, substitute a
single Writeln statement for the IF statement below.
}
begin
if Line > 20 then
begin
GoToXY(1, 5); { first line below header }
DelLine;
GoToXY(1, 24); { last line on screen }
end
else
begin
GoToXY(1, Line + 4);
end;
end; { Scroll }
begin
Write(^G); { ring bell -- finished w/ sort! }
ClrEOS(5); { clear from line 5 to end of screen }
Line := 1; { init line count }
case Choice of { retrieve records from sort & display }
'C' : begin
repeat
if KeyPressed then Halt; { Key touched? Stop program }
Scroll(Line);
SortReturn(Customer);
with Customer do
begin
Write(Line:3, Number:6, ' ', Name,' ');
for i := Length(Name) to 25 do Write(' ');
Write(Addr);
for i := Length(Addr) to 20 do Write(' ');
Write(City);
for i := Length(City) to 12 do Write(' ');
Write(State,' ', Zip);
end; { with }
Line := Line + 1;
until SortEOS;
end; { C }
'S' : begin
repeat
if KeyPressed then Halt; { Key touched? Stop program }
SortReturn(Item);
Scroll(Line);
with Item do
begin
Write(Line:13, Number:6, ' ', Descrip,' ');
for i := Length(Descrip) to 30 do Write(' ');
Write(InStock:5, Price:8:2);
end;
Line := Line + 1;
until SortEOS;
end; { S }
end; { case }
Scroll(25); { make room for results message }
Scroll(25);
Scroll(25);
end; { OutP }
procedure DisplayResults(Results : integer);
begin
case Results of { display sort results }
0 : Write('Done with sort and display.');
3 : Write('Error: not enough memory to sort');
8 : Write('Error: illegal item length.');
9 : Write('Error: can only sort ', MaxInt, ' records.');
10 : Write('Error: disk full or disk write error.');
11 : Write('Error: disk error during read.');
12 : Write('Error: directory full or invalid path name');
end; { case }
end; { DisplayResults }
begin { program body }
OpenFile(Choice); { open data file to sort }
case Choice of { sort the file of records }
'C' : Results := TurboSort(SizeOf(CustRec)); { customer file }
'S' : Results := TurboSort(SizeOf(ItemRec)); { stock file }
end; { case }
DisplayResults(Results); { display sort results }
end.